
//% color="#6495ED" iconWidth=50 iconHeight=40
namespace Sklearn_linear{

    //% block="初始化线性回归" blockType="command"
    
    export function Sklearn_init(parameter: any, block: any) {
        // let type=parameter.TYPE.code; 
        // if(`${type}` === '1'){
        Generator.addImport(`from sklearn.linear_model import LinearRegression\nimport numpy as np\nimport matplotlib.pyplot as plt`)
        // }else if(`${type}` === '2'){//% TYPE.shadow="dropdown" TYPE.options="TYPE"
        //     Generator.addImport(`from sklearn.linear_model import LinearRegression\nfrom sklearn.preprocessing import PolynomialFeatures\nimport numpy as np`)
        // }
    }

    //% block="实例化 线性回归直线，训练X轴数据集[X_DATA]Y轴数据集[Y_DATA]" blockType="command"
    //% X_DATA.shadow="normal" X_DATA.defl="X_DATA"
    //% Y_DATA.shadow="normal" Y_DATA.defl="Y_DATA"
    export function train_test_split(parameter: any, block: any) {
        let x=parameter.X_DATA.code;
        let y=parameter.Y_DATA.code;

        Generator.addCode(`Sklearn_linear_model = LinearRegression()\nSklearn_linear_model.fit(np.array(${x}).reshape(-1, 1),np.array(${y}))`)

    }

    //% block="预测数据[DATA]" blockType="reporter"
    //% DATA.shadow="normal" DATA.defl="20"
 
    export function Sklearn_initpredict(parameter: any, block: any) {
 
        let data=parameter.DATA.code;  
 
        Generator.addCode(`round(Sklearn_linear_model.predict(np.array([[${data}]]))[0],2)`)
    } 
    //% block="---"
    export function noteSep2() {

    }
    //% block="标题[TEXT]字号[NUMBER]字体[FONT]" blockType="command"
    //% TEXT.shadow="string" TEXT.defl="标题"
    //% NUMBER.shadow="normal" NUMBER.defl="15"
    //% FONT.shadow="dropdown" FONT.options="FONT"
    export function matplotlib_title(parameter: any, block: any) {
 
        let te=parameter.TEXT.code;  
        let nu=parameter.NUMBER.code;  
        let font =parameter.FONT.code;  
        if(`${font}`===`HYQiHei`){
         
            Generator.addCode(`plt.figure(figsize=(8, 7),dpi=90)\nplt.subplots_adjust(left=20/100,bottom=15/100)\nplt.rcParams["font.sans-serif"] = ["${font}"]\nplt.title(${te},color='black',size="${nu}")`) 
        }else{

            Generator.addCode(`plt.rcParams["font.sans-serif"] = ["${font}"]\nplt.title(${te},color='black',size="${nu}")`) 
  
        }
    } 
    //% block="标签[XY]标题[TEXT]字号[SIZE]" blockType="command"
    //% XY.shadow="dropdown" XY.options="XY"
    //% TEXT.shadow="string" TEXT.defl="X轴"
    //% SIZE.shadow="number" SIZE.defl="10"
    export function matplotlib_label(parameter: any, block: any) {
        let xy=parameter.XY.code;
        let text=parameter.TEXT.code;
        let size=parameter.SIZE.code;
        Generator.addCode(`plt.${xy}label(${text}, fontsize=${size})`)
             
    }

    //% block="真实数据[X][Y] 符号标记[TYPE1] 颜色r[R]g[G]b[B]标题[TEXT]" blockType="command"
    //% X.shadow="normal" X.defl="X_DATA"
    //% Y.shadow="normal" Y.defl="Y_DATA"
    //% TYPE1.shadow="dropdown" TYPE1.options="TYPE1"
    //% R.shadow="number" R.defl="255"
    //% G.shadow="number" G.defl="0"
    //% B.shadow="number" B.defl="0"
    //% TEXT.shadow="string" TEXT.defl="标题"
    export function matplotlib_label_1(parameter: any, block: any) {
        let x=parameter.X.code;
        let y=parameter.Y.code;
        let TYPE1=parameter.TYPE1.code;
        let r=parameter.R.code;
        let g=parameter.G.code;
        let b=parameter.B.code;

        let text=parameter.TEXT.code;
 
        Generator.addCode(`plt.scatter( ${x}, ${y}, color=(${r}/255,${g}/255,${b}/255),marker=${TYPE1},label=${text})\n`)
             
    }  
    //% block="预测线性回归直线 [X][Y] 符号标记[TYPE2] 颜色r[R]g[G]b[B]标题[TEXT]" blockType="command"
    //% X.shadow="normal" X.defl="X_DATA"
    //% Y.shadow="normal" Y.defl="Y_DATA"
    //% TYPE2.shadow="dropdown" TYPE2.options="TYPE2"
    //% R.shadow="number" R.defl="0"
    //% G.shadow="number" G.defl="0"
    //% B.shadow="number" B.defl="255"
    //% TEXT.shadow="string" TEXT.defl="标题"
    export function matplotlib_label_2(parameter: any, block: any) {
        let x=parameter.X.code;
        let y=parameter.Y.code;
        let TYPE1=parameter.TYPE2.code;
        let r=parameter.R.code;
        let g=parameter.G.code;
        let b=parameter.B.code;

        let text=parameter.TEXT.code;
 
        Generator.addCode(`X_value = np.array(${x}).reshape(-1, 1)\ny_value = np.array(${x})\nX_fit = np.linspace(X_value.min(), y_value.max(), 100).reshape(-1, 1)\ny_fit = Sklearn_linear_model.predict(X_fit)\nplt.plot( X_fit, y_fit, color=(${r}/255,${g}/255,${b}/255),linestyle=${TYPE1},label=${text})`)
             
    }   
    //% block="显示图表" blockType="command"

    export function matplotlib_label_3(parameter: any, block: any) {
        Generator.addCode(`plt.legend(fontsize=8)\nplt.show()`)
    }
}


